const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tops:[],
    currentTab:0,
    menus:[],
    menuWidth:0,
    bottoms:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTopData()
    this.getMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  menuClick:function(e){
    console.log(e)
    var key = Number(e.currentTarget.id)
    this.setData({
      currentTab:key
    })

    var obj = this.data.menus[key]
    this.getBoottoms(obj)
  },
  getTopData: function () {
    var that = this
    that.setData({
      source: []
    })
    wx.request({
      url: app.net.baseURL + '/api/recommend/queryProductWithCommodityListByRecommendInfo',
      header: app.net.header,
      method: 'POST',
      data: {
        "source": '20'
      },
      success: function (res) {
        that.setData({
          tops: res.data.data
        })
      },
      fail: function (e) {
        console.log(e)
      }
    })
  },

  getMenu: function () {
    var that = this
    wx.request({
      url: app.net.baseURL + '/api/recommend/queryRecommendList',
      header: app.net.header,
      method: 'POST',
      data: {
        "source": '30'
      },
      success: function (res) {
        console.log(res.data.data)

        var count = res.data.data.length
        var width = 0
        wx.getSystemInfo({
          success: function(res) {
            width = res.windowWidth
          },
        })

        that.setData({
          menus: res.data.data,
          menuWidth:width
        })
        that.getBoottoms(res.data.data[0])
      },
      fail: function (e) {
        console.log(e)
      }
    })
  },
  getBoottoms: function (obj) {
    var that = this
    that.setData({
      source: []
    })
    wx.request({
      url: app.net.baseURL + '/api/recommend/queryProductWithCommodityListByRecommendInfo',
      header: app.net.header,
      method: 'POST',
      data: {
        "id": obj.id,
        "source": obj.source
      },
      success: function (res) {
        console.log(res)
        that.setData({
          bottoms: res.data.data
        })
      },
      fail: function (e) {
        console.log(e)
      }
    })
  }
})